<template>
	<view class="content">
		<view class="wid90 mar dis disAl" @click="guanliClick()" style="justify-content: flex-end;height: 100rpx;">管理</view>
		<view v-if="list.length == 0 && list1.length == 0" class="wid90 mar fowe800 foSi35">
			<view style="height: 50rpx;"></view>
			<view class="dis disJuC">
				<image src="../../static/zanwu2.png" style="width: 400rpx;height: 300rpx;"></image>
			</view>
			<view class="textCen mar-top20" style="color: #868686;">暂无数据</view>
		</view>
		<view v-if="list.length > 0 || list1.length > 0" class="wid90 mar borRad20" style="box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0,0,0,0.1);">
			<view v-if="list.length > 0">
				<view class="wid90 mar dis disAl foSi40 fowe600" style="height: 96rpx;">洗护</view>
				<view class="wid100" style="border-bottom: 1rpx dashed #E1DFDF;"></view>
				<block v-for="(item,index) in list" :key="index">
					<view class="wid90 mar dis disAl" style="padding: 20rpx 0;" @click="xuanClick(index,0)">
						<view class="dis disAl wid30">
							<view v-if="item.biaoji == 1 && biaoji == 0"
								class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #37BCC3;">
								<view class="borRad50" style="width: 16rpx;height: 16rpx;background-color: #37BCC3;"></view>
							</view>
							<view v-if="(item.biaoji == 0 && biaoji == 0) || biaoji == 1"
								class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #818181;"></view>
							<view class="mar-left20" style="height: 115rpx;">
								<image :src="url+item.goods.pic.split(',')[0]" style="width: 115rpx;" class="hei100"></image>
							</view>
						</view>
						<view class="wid70">
							<view class="fowe600 foSi30">{{item.goods.name}}</view>
							<view class="foSi22" style="color: #868686;">{{item.GoodsSpecifications.name}}</view>
							<view class="dis disAl disJuB">
								<view class="foSi30" style="color: #37BCC3;">¥{{item.GoodsSpecifications.price}}</view>
								<view class="dis disAl">
									<image src="../../static/jian.png" @click.stop="jianClick(index,0)"
										style="width: 40rpx;height: 40rpx;"></image>
									<view class="textCen foSi30" style="width: 60rpx;">{{item.nums}}</view>
									<image src="../../static/jia.png" @click.stop="jiaClick(index,0)"
									style="width: 40rpx;height: 40rpx;"></image>
								</view>
							</view>
						</view>
					</view>
				</block>
				<view class="wid100" style="border-bottom: 1rpx dashed #E1DFDF;"></view>
			</view>
			<view v-if="list1.length > 0">
				<view class="wid90 mar dis disAl foSi40 fowe600" style="height: 96rpx;">商城</view>
				<view class="wid100" style="border-bottom: 1rpx dashed #E1DFDF;"></view>
				<block v-for="(item,index) in list1" :key="index">
					<view class="wid90 mar dis disAl" style="padding: 20rpx 0;" @click="xuanClick(index,1)">
						<view class="dis disAl wid30">
							<view v-if="item.biaoji == 1 && biaoji == 1"
								class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #37BCC3;">
								<view class="borRad50" style="width: 16rpx;height: 16rpx;background-color: #37BCC3;"></view>
							</view>
							<view v-if="(item.biaoji == 0 && biaoji == 1) || biaoji == 0"
								class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #818181;"></view>
							<view class="mar-left20" style="height: 115rpx;">
								<image :src="url+item.goods.pic.split(',')[0]" style="width: 115rpx;" class="hei100"></image>
							</view>
						</view>
						<view class="wid70">
							<view class="fowe600 foSi30">{{item.goods.name}}</view>
							<view class="dis disAl disJuB mar-top30">
								<view class="foSi30" style="color: #37BCC3;">¥{{item.goods.price}}</view>
								<view class="dis disAl">
									<image src="../../static/jian.png" @click.stop="jianClick(index,1)"
										style="width: 30rpx;height: 30rpx;"></image>
									<view class="textCen foSi30" style="width: 60rpx;">{{item.nums}}</view>
									<image src="../../static/jia.png" @click.stop="jiaClick(index,1)"
									style="width: 30rpx;height: 30rpx;"></image>
								</view>
							</view>
						</view>
					</view>
				</block>
				<view class="wid100" style="border-bottom: 1rpx dashed #E1DFDF;"></view>
			</view>
			<view class="wid90 mar dis disAl disJuB foSi40 fowe600" @click="quanxuanClick()" style="height: 96rpx;">
				<view class="dis disAl">
					<view v-if="quanxuan"
						class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #37BCC3;">
						<view class="borRad50" style="width: 16rpx;height: 16rpx;background-color: #37BCC3;"></view>
					</view>
					<view v-if="!quanxuan"
						class="dis disAl disJuC borRad50" style="width: 28rpx;height: 28rpx;border: 4rpx solid #818181;"></view>
					<view class="mar-left10 foSi25">全选</view>
				</view>
				<view v-if="!guanli" class="dis disAl">
					<view class="foSi25">总计：<span style="color: #37BCC3;">¥{{money}}</span></view>
					<view class="dis disAl disJuC foSi30 coFFF hei60 borRad40 mar-left10" @click.stop="jiesuan()"
					style="width: 150rpx;background-color: #37BCC3;">去结算</view>
				</view>
				<view v-if="guanli" class="dis disAl disJuC foSi30 coFFF hei60 borRad40 mar-left10" @click.stop="shanchu()"
				style="width: 150rpx;background-color: #FF0000;">删除</view>
			</view>
		</view>
		<view style="height: 50rpx;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				quanxuan: false,
				list: [],
				list1: [],
				biaoji: 0,
				guanli: false,
				money: 0
			}
		},
		onLoad() {},
		onShow() {
			this.getList()
		},
		methods: {
			jisuan(){
				var money = 0
				if(this.biaoji == 0){
					for (var i=0; i<this.list.length; i++) {
						if(this.list[i].biaoji == 1){
							money += this.list[i].GoodsSpecifications.price * this.list[i].nums
						}
					}
				}else{
					for (var i=0; i<this.list1.length; i++) {
						if(this.list1[i].biaoji == 1){
							money += this.list1[i].goods.price * this.list1[i].nums
						}
					}
				}
				this.money = money
				this.$forceUpdate();
			},
			getList(){
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_cars',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						"token": uni.getStorageSync('token')
					},
					data: {
						gas_station_id: uni.getStorageSync('jiaId')
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.list = result.data.xihu
							that.list1 = result.data.shangcheng
							for (var i=0; i<that.list.length; i++) {
								that.list[i].biaoji = 0
							}
							for (var i=0; i<that.list1.length; i++) {
								that.list1[i].biaoji = 0
							}
							if(that.list.length == 0){
								that.biaoji = 1
							}
						}
					}
				});
			},
			shanchu(){
				var that = this;
				var id = ''
				if(that.biaoji == 0){
					for (var i=0; i<that.list.length; i++) {
						if(that.list[i].biaoji == 1){
							id += id != ''?','+that.list[i].id:that.list[i].id
							that.list.splice(i,1);
							i -= 1
						}
					}
				}else{
					for (var i=0; i<that.list1.length; i++) {
						if(that.list1[i].biaoji == 1){
							id += id != ''?','+that.list1[i].id:that.list1[i].id
							that.list1.splice(i,1);
							i -= 1
						}
					}
				}
				that.guanli = false
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_cars_del',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						"token": uni.getStorageSync('token')
					},
					data: {
						id: id
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							uni.showToast({title: "删除成功",icon: 'error',duration: 2000})
							this.money = 0
							this.$forceUpdate();
						}
					}
				});
			},
			guanliClick(){
				this.guanli = !this.guanli
				this.$forceUpdate();
			},
			jiesuan(){
				var id = ''
				if(this.biaoji == 0){
					for (var i=0; i<this.list.length; i++) {
						if(this.list[i].biaoji == 1){
							id += id?','+this.list[i].id:this.list[i].id
						}
					}
					this.tiaozhuan('/pages/toiletries/exchange?id='+id+'&is_pd=1')
				}else{
					for (var i=0; i<this.list1.length; i++) {
						if(this.list1[i].biaoji == 1){
							id += id?','+this.list1[i].id:this.list1[i].id
						}
					}
					this.tiaozhuan('/pages/commodity/exchange?id='+id+'&is_pd=1'	)
				}
			},
			quanxuanClick(){
				if(this.quanxuan){
					if(this.biaoji == 0){
						for (var i=0; i<this.list.length; i++) {
							this.list[i].biaoji = 0
						}
					}else{
						for (var i=0; i<this.list1.length; i++) {
							this.list1[i].biaoji = 0
						}
					}
				}else{
					if(this.biaoji == 0){
						for (var i=0; i<this.list.length; i++) {
							this.list[i].biaoji = 1
						}
					}else{
						for (var i=0; i<this.list1.length; i++) {
							this.list1[i].biaoji = 1
						}
					}
				}
				this.quanxuan = !this.quanxuan
				this.jisuan()
			},
			xuanClick(index,biaoji){
				this.quanxuan = false
				if(this.biaoji == biaoji){
					if(biaoji == 0){
						this.list[index].biaoji = this.list[index].biaoji==0?1:0
					}else{
						this.list1[index].biaoji = this.list1[index].biaoji==0?1:0
					}
				}else{
					this.biaoji = biaoji
					for (var i=0; i<this.list.length; i++) {
						this.list[i].biaoji = 0
					}
					for (var i=0; i<this.list1.length; i++) {
						this.list1[i].biaoji = 0
					}
					if(biaoji == 0){
						this.list[index].biaoji = 1
					}else{
						this.list1[index].biaoji = 1
					}
				}
				this.jisuan()
				this.$forceUpdate();
			},
			jianClick(index,biaoji){
				if(biaoji == 0 && this.list[index].nums > 1){
					this.list[index].nums -= 1
					this.xiugai(this.list[index].id,this.list[index].nums)
				}else if(biaoji == 1 && this.list1[index].nums > 1){
					this.list1[index].nums -= 1
					this.xiugai(this.list1[index].id,this.list1[index].nums)
				}
			},
			jiaClick(index,biaoji){
				if(biaoji == 0){
					this.list[index].nums += 1
					this.xiugai(this.list[index].id,this.list[index].nums)
				}else if(biaoji == 1){
					this.list1[index].nums += 1
					this.xiugai(this.list1[index].id,this.list1[index].nums)
				}
				
			},
			xiugai(id,nums){
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_cars_upd',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						"token": uni.getStorageSync('token')
					},
					data: {
						id: id,
						nums: nums
					},
					success(res) {
						that.jisuan()
					}
				});
			},
			tiaozhuan(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style>
	.content {}
</style>